﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;

using System.Security.Cryptography;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;

namespace CaseStudy.AdminPortal
{
    public partial class UserInfoDetail : System.Web.UI.Page
    {
        private bool isEdit = true;
        private string id = string.Empty;
        private string mode = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            if (Request.QueryString["mode"] != null)
                mode = Request.QueryString["mode"].ToLower();

            if (Request.QueryString["id"] != null)
            {
                id = Request.QueryString["id"];
                if (!IsPostBack)
                {
                    BindData();
                }

                if (mode.Equals("edit"))
                {
                    isEdit = true;


                    currentMapItem.InnerText = "编辑用户";
                }
                else
                {
                    isEdit = false;
                    currentMapItem.InnerText = "查看用户";

                    txtUserName.Enabled = false;
                    divPassword.Visible = false;
                    divConfirm.Visible = false;
                    txtNickName.Enabled = false;

                    imgBtnConfirm.Visible = false;
                    imgBtnReset.Visible = false;
                }
            }
            else
            {
                isEdit = false;
                currentMapItem.InnerText = "创建用户";
            }
        }

        protected void imgBtnReset_Click(object sender, EventArgs e)
        {
            divReminding.InnerHtml = "";
            divReminding.Visible = false;

            if (!isEdit)
            {
                txtUserName.Text = string.Empty;
                txtPassword.Text = string.Empty;
                txtConfirm.Text = string.Empty;
                txtNickName.Text = string.Empty;
            }
            else
            {
                BindData();
            }
        }

        protected void imgBtnConfirm_Click(object sender, EventArgs e)
        {
            if (!ValidateDates())
                return;

            string userName = HttpUtility.HtmlEncode(txtUserName.Text);
            string password = HttpUtility.HtmlEncode(txtPassword.Text);
            string confirm = HttpUtility.HtmlEncode(txtConfirm.Text);
            string nickName = HttpUtility.HtmlEncode(txtNickName.Text);

            UserInfo userInfo = new UserInfo();
            userInfo.Name = userName;
            userInfo.Password = Encypt(password);
            userInfo.NickName = nickName;
            userInfo.ModifyTime = DateTime.Now;

            string message = string.Empty;

            if (!isEdit)
            {
                userInfo.CreateTime = DateTime.Now;
                if (!UserInformationBLL.Insert(userInfo, out message))
                {
                    divReminding.InnerHtml = message;
                    divReminding.Visible = true;
                    divReminding.Style.Add("color", "Red");
                    return;
                }
                else
                {
                    divReminding.InnerHtml = "成功添加用户！";
                    divReminding.Visible = true;
                    divReminding.Style.Add("color", "Green");
                    return;
                }
            }
            else
            {
                userInfo.Id = id;
                if (!UserInformationBLL.Update(userInfo, out message))
                {
                    divReminding.InnerHtml = message;
                    divReminding.Visible = true;
                    divReminding.Style.Add("color", "Red");
                    return;
                }
                else
                {
                    divReminding.InnerHtml = "成功修改用户！";
                    divReminding.Visible = true;
                    divReminding.Style.Add("color", "Green");
                    return;
                }
            }

            //Response.Redirect("UserManager.aspx");
        }

        protected void imgBtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserManager.aspx");
        }

        private void BindData()
        {
            UserInfo userInfo = UserInformationBLL.GetInfo(id);

            txtUserName.Text = HttpUtility.HtmlDecode(userInfo.Name);
            txtNickName.Text = HttpUtility.HtmlDecode(userInfo.NickName);
        }

        private string Encypt(string key)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(key);
            byte[] OutBytes = md5.ComputeHash(InBytes);
            string OutString = "";
            for (int i = 0; i < OutBytes.Length; i++)
            {
                OutString += OutBytes[i].ToString("x2");
            }
            return OutString;
        }

        private bool ValidateDates()
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                divReminding.InnerHtml = "用户名不能为空！";
                divReminding.Visible = true;
                divReminding.Style.Add("color", "Red");
                return false;
            }

            if (HttpUtility.HtmlEncode(txtUserName.Text).Length > 49)
            {
                divReminding.InnerHtml = "用户名太长了！";
                divReminding.Style.Add("color", "Red");
                divReminding.Visible = true;
                return false;
            }

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                divReminding.InnerHtml = "密码不能为空！";
                divReminding.Visible = true;
                divReminding.Style.Add("color", "Red");
                return false;
            }

            if (HttpUtility.HtmlEncode(txtPassword.Text).Length > 49)
            {
                divReminding.InnerHtml = "密码太长了！";
                divReminding.Style.Add("color", "Red");
                divReminding.Visible = true;
                return false;
            }

            if (!txtConfirm.Text.Equals(txtPassword.Text))
            {
                divReminding.InnerHtml = "密码确认错误！";
                divReminding.Visible = true;
                divReminding.Style.Add("color", "Red");
                return false;
            }

            if (string.IsNullOrEmpty(txtNickName.Text))
            {
                divReminding.InnerHtml = "昵称不能为空！";
                divReminding.Visible = true;
                divReminding.Style.Add("color", "Red");
                return false;
            }

            if (HttpUtility.HtmlEncode(txtNickName.Text).Length > 49)
            {
                divReminding.InnerHtml = "昵称太长了！";
                divReminding.Style.Add("color", "Red");
                divReminding.Visible = true;
                return false;
            }

            return true;
        }
    }
}
